package com.hehui.partimejob.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.hehui.partimejob.R;
import com.hehui.partimejob.fragment.category_child.OneAreaFragment;
import com.hehui.partimejob.fragment.category_child.ShareToCoinFragment;
import com.hehui.partimejob.service.model.CategoryModel;

import java.util.ArrayList;
import java.util.List;

public class CategoryFragment extends BaseFragment {

    private TabLayout cateFunNav;
    private ViewPager2 vp;


    private List<CategoryModel> categoryModelList;


    @Override
    public void init() {
        getNav().getNav().setBackgroundColor(A.getColor(R.color.white));
        getNav().setTitle(R.string.category).setTitleColor(A.getColor(R.color.black)).isShowBack(false);

        cateFunNav = (TabLayout) findViewById(R.id.cateFunNav);
        vp = (ViewPager2) findViewById(R.id.vp);


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_category;
    }

    @Override
    protected void begin(Bundle bundle) {
        categoryModelList = new ArrayList<>();
        List<Fragment> mainFragmentList = new ArrayList<>();
        mainFragmentList.add(new OneAreaFragment());
        mainFragmentList.add(new ShareToCoinFragment());

        vp.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return mainFragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return mainFragmentList.size();
            }
        });

        vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                cateFunNav.selectTab(cateFunNav.getTabAt(position));
            }
        });



        String[] stringArray = getResources().getStringArray(R.array.cate_nav_title);
        for (String s : stringArray) {
            cateFunNav.addTab(cateFunNav.newTab().setText(s));
        }
        cateFunNav.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                vp.setCurrentItem(cateFunNav.getSelectedTabPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }
}
